#include "stdafx.h"
#include "rope.h"
#include "bridge.h"

using namespace Ogre;

namespace HeightFunction
{
	Real pos[6][6]={21,21,19.5,19.6,19,19,
				   19,20,19.2,19.5,19.2,20,
				   20,20.2,21,21.5,21,20,
				  20,20.5,22.5,22.4,22,20, 
				   20,20.5,22,22,21,20,
				   21,21,19.5,19.6,19,19};

	int border[2]={22,318};
		int maxX=51/5;
		int maxZ=59/5;
		inline float getTerrainHeight(const float x, const float z, void *userData = NULL){
		Real x0=(x-border[0])/maxX;
	    Real z0=(z-border[1])/maxZ;
		int xC=Math::Ceil(x0);
	    int zC=Math::Ceil(z0);
		int xF=Math::Floor(x0);
		int zF=Math::Floor(z0);
		Real xp=x0-xF;
		Real zp=z0-zF;
		Real xpc=xC-x0;
		Real zpc=zC-z0;
		int y=pos[xF][zF]*(1-xp)*(1-zp)+pos[xC][zC]*(1-xpc)*(1-zpc)+pos[xF][zC]*(1-xp)*(1-zpc)+pos[xC][zF]*(1-xpc)*(1-zp);
		return y;
	}
}
OgreNewt::World* www=NULL;

namespace HeightFunction2
{
   Real pos[11][11]={2, 2,  2,   2,  2,    3,  2,   2, 2,   2, 2,
				     2, 3.5,  4 ,  4,  4,    5,  4 ,  4, 4,   3.5, 2,
				     2, 4,  5.5,   6,  6, 7.5,   6,   6, 5.5,   4, 2,
				     2, 4,  6  , 8, 8.5,  9,   8.5, 8, 6  , 4, 2,
				     2, 4,  6, 8.5, 10,  10.5, 10 , 8.5, 6, 4, 3,
				     3, 5,  7.5, 9, 10.5,11,  10.5, 9, 7.5, 5, 3,///
				     2, 4,  6, 8.5, 10,  10.5, 10 , 8.5, 6, 4, 3,
					 2, 4,  6  , 8, 8.5,  9,   8.5, 8, 6  , 4, 2,
					 2, 4,  5.5,   6,  6, 7.5,   6,   6, 5.5,   4, 2,
					 2, 3.5,  4 ,  4,  4,    5,  4 ,  4, 4,   3.5, 2,
					 2, 2,  2,   2,  2,    3,  2,   2, 2,   2, 2};

	int border[2]={-20,-20};
		int maxX=40/10;
		int maxZ=40/10;
		inline float getTerrainHeight(const float x, const float z, void *userData = NULL){
		Real d=-333;	

OgreNewt::BasicRaycast ray( www,Vector3(x,20,z),Vector3(x,-100,z)  ,true);
		OgreNewt::BasicRaycast::BasicRaycastInfo info = ray.getInfoAt(0);

			if (info.mBody)
			{
				d=20+((-120)*info.mDistance);
			}
		Sleep(0.5);


		return d;
	}
}

void createMenuLevel(SceneManager* mSceneMgr, OgreNewt::World* mWorld,OgreNewt::MaterialID* flag_mat,LvlUpdate** lvlUp)
{
	www=mWorld;
	loadScene("../../media/menu.scene",mSceneMgr,mWorld,NULL,flag_mat);
	mSceneMgr->setSkyBox(true, "TCENoonSkyBox");
	Camera* mCamera=mSceneMgr->getCamera("Camera"); 
	SceneNode* camRotator=mSceneMgr->getRootSceneNode()->createChildSceneNode("camRotator");
	camRotator->attachObject(mCamera);
	camRotator->setPosition(Vector3(0,15,0));
	mCamera->setPosition(Vector3(-15,-9,15));
	mCamera->lookAt(Vector3(0,15,0));
	camRotator->yaw(Ogre::Degree(-65));
	
	Forests::PagedGeometry *grass = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	grass->addDetailLevel<Forests::GrassPage>(200,20);
	Forests::GrassLoader *grassLoader = new Forests::GrassLoader(grass);
	grassLoader->setHeightFunction(&HeightFunction2::getTerrainHeight);
	grass->setPageLoader(grassLoader);
	Forests::GrassLayer *layer = grassLoader->addLayer("grassHop");
	layer->setMinimumSize(2.0f, 2.0f);
	layer->setMaximumSize(2.5f, 2.5f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(1.8f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(-30, -30,30,30));
	//layer->setColorMap("lm.png");
	layer = grassLoader->addLayer("grassHop");
	layer->setMinimumSize(1.0f, 1.0f);
	layer->setMaximumSize(1.5f, 1.5f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(0.3f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(-30, -30,30,30));

	*lvlUp= new LvlMenuUpdate(camRotator,grass);
}

void createLevel2(SceneManager* mSceneMgr, OgreNewt::World* mWorld, EnvironmentSounds* env,OgreNewt::MaterialID* flag_mat,LvlUpdate** lvlUp)
{


	Rope* rr=new Rope(mSceneMgr,mWorld,Vector3(13,11,6),10,0.5);
	
	mWorld->setWorldSize(Vector3(-2000,-500,-2000),Vector3(2000,500,2000));
	loadScene("../../media/testxml.scene",mSceneMgr,mWorld,env,flag_mat);
	mSceneMgr->setSkyBox(true, "TCENoonSkyBox");
Bridge* bb=new Bridge(mSceneMgr,mWorld,Vector3(-21.5,-36.8,78),20,1);

/*
	Entity *myTree = mSceneMgr->createEntity("MyTree", "default.mesh");
	myTree->setMaterialName("woody");
	myTree->setCastShadows(false);

	Forests::PagedGeometry *trees = new Forests::PagedGeometry();
	trees->setCamera(mSceneMgr->getCamera("Camera"));	//Set the camera so PagedGeometry knows how to calculate LODs
	trees->setPageSize(80);	//Set the size of each page of geometry
	trees->setInfinite();		//Use infinite paging mode
	trees->addDetailLevel<Forests::BatchPage>(150, 50);		//Use batches up to 150 units away, and fade for 30 more units
	trees->addDetailLevel<Forests::ImpostorPage>(500, 50);	//Use impostors up to 400 units, and for for 50 more units
	Forests::TreeLoader3D *treeLoader = new Forests::TreeLoader3D(trees, Forests::TBounds(0, 0, 1500, 1500));
	trees->setPageLoader(treeLoader);	//Assign the "treeLoader" to be used to load geometry for the PagedGeometry instance
	
*/



	// Ogre::StaticGeometry *sg = mSceneMgr->createStaticGeometry("GrassArea");
	// sg->setRegionDimensions(Ogre::Vector3(200, 200, 200));
//sg->setOrigin(Ogre::Vector3(0, 0, 0));

	for(int i=-5;i<5;i++)
		for(int j=0;j<10;j++)
		{
			/*Ogre::MeshPtr m=MeshManager::getSingleton().load( "default.mesh", "General");
			Ogre::MeshPtr m2=MeshManager::getSingleton().load( "defaultv2.mesh", "General");
			m.get()->createManualLodLevel(50,m2.get()->getName());*/
			Entity* e=mSceneMgr->createEntity(Ogre::StringConverter::toString(j)+Ogre::StringConverter::toString(i)+"aaa","default.mesh");

			OgreNewt::BasicRaycast ray (www,Vector3(i*25,100,j*25),Vector3(i*25,-50,j*25)  ,true);
		OgreNewt::BasicRaycast::BasicRaycastInfo info = ray.getInfoAt(0);
			Real d=0;
			if (info.mBody)
			{
				d=(100+(-150)*info.mDistance);
			}
			
			Quaternion q;
			q.FromAngleAxis(Ogre::Radian(Ogre::Math::RangeRandom(0,3)),Vector3(0.1,1,0.1));
			if(d!=0){
			SceneNode* s=mSceneMgr->getRootSceneNode()->createChildSceneNode();
			s->attachObject(e);
			s->setPosition(Vector3(i*25,d , j*25));
			s->setOrientation(q);
				//treeLoader->addTree(e, Vector3(i*25,d , j*25), q.getYaw(), 1);
			}

			//sg->addEntity(e, Vector3(i*25,d , j*25),q , Vector3(1,1,1));
		}

		//sg->setCastShadows(true);
		//sg->build();

/*
	Forests::PagedGeometry *grass = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	grass->addDetailLevel<Forests::GrassPage>(200,20);
	Forests::GrassLoader *grassLoader = new Forests::GrassLoader(grass);
	grassLoader->setHeightFunction(&HeightFunction::getTerrainHeight);
	grass->setPageLoader(grassLoader);*/
			/*
		SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
        Animation* anim = mSceneMgr->createAnimation("CameraTrack", 40);
        anim->setInterpolationMode(Animation::IM_SPLINE);
        NodeAnimationTrack* track = anim->createNodeTrack(0, node);
        track->createNodeKeyFrame(0)->setTranslate(Vector3(200, 100, 0));
        track->createNodeKeyFrame(10)->setTranslate(Vector3(0, 100, 200));
        track->createNodeKeyFrame(20)->setTranslate(Vector3(-200, 100, 0));
        track->createNodeKeyFrame(30)->setTranslate(Vector3(0, 100, -200));
        track->createNodeKeyFrame(40)->setTranslate(Vector3(200, 100, 0));
        AnimationState* mAnimState = mSceneMgr->createAnimationState("CameraTrack");
        mAnimState->setEnabled(true);*/
		//p->setBaloonTrack(node);

		//Baloon* b=new Baloon(mSceneMgr,mWorld,Vector3(0,20,0));

	/*Forests::GrassLayer *layer = grassLoader->addLayer("grass");
	layer->setMinimumSize(2.0f, 2.0f);
	layer->setMaximumSize(2.5f, 2.5f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(1.5f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(22, 318,81,368));
	layer->setDensityMap("teren.png");
	//layer->setColorMap("lm.png");
	layer = grassLoader->addLayer("grass");
	layer->setMinimumSize(2.0f, 2.0f);
	layer->setMaximumSize(2.5f, 2.5f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(0.5f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(22, 318,81,368));
	//layer->setColorMap("lm.png");
	layer->setDensityMap("teren.png");*/

	Forests::PagedGeometry *grass = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	grass->addDetailLevel<Forests::GrassPage>(120,5);
	//grass->addDetailLevel<Forests::ImpostorPage>(200,25);
	Forests::GrassLoader *grassLoader = new Forests::GrassLoader(grass);
	grassLoader->setHeightFunction(&HeightFunction2::getTerrainHeight);
	grass->setPageLoader(grassLoader);
	Forests::GrassLayer *layer = grassLoader->addLayer("grassHop");
	layer->setMinimumSize(1.5f, 1.5f);
	layer->setMaximumSize(2.0f, 2.0f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(3.0f);
	layer->setSwayLength(0.3f);
	layer->setSwaySpeed(0.3f);
	layer->setDensity(2.0f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(-300, -300,300,300));
	layer->setMaxSlope(Ogre::Radian(Ogre::Degree(50)));

/*
	int o=0;
	for(int i=0;i<6;i++)
		for(int j=0;j<3
			;j++)
		{

		
		OgreNewt::BasicRaycast ray(www,Vector3(i*15,0,j*15),Vector3(i*15,-50,j*15)  ,true);
		OgreNewt::BasicRaycast::BasicRaycastInfo info = ray.getInfoAt(0);

			if (info.mBody)
			{
			Entity *myTree = mSceneMgr->createEntity("MyTree"+Ogre::StringConverter::toString(o++), "default.mesh");
			Ogre::SceneNode* nn=mSceneMgr->getRootSceneNode()->createChildSceneNode(Vector3(i*15,(-50)*info.mDistance+7.5, j*15));
			nn->attachObject(myTree);
			nn->yaw(Ogre::Radian(Ogre::Degree(180)));
			}	
		}*/

	//loadScene("../../media/testxml2.scene",mSceneMgr,mWorld,NULL,flag_mat);

	SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    Animation* anim = mSceneMgr->createAnimation("CameraTrack", 15);
    anim->setInterpolationMode(Animation::IM_SPLINE);
    NodeAnimationTrack* track = anim->createNodeTrack(0, node);
    track->createNodeKeyFrame(0)->setTranslate(Vector3(100, 50, 0));
    track->createNodeKeyFrame(5)->setTranslate(Vector3(0, 150, 100));
    track->createNodeKeyFrame(10)->setTranslate(Vector3(-100, 50, 0));
    track->createNodeKeyFrame(12)->setTranslate(Vector3(0, 30, 0));
    track->createNodeKeyFrame(15)->setTranslate(Vector3(100, 50, 0));
    AnimationState* mAnimState = mSceneMgr->createAnimationState("CameraTrack");
    mAnimState->setEnabled(true);

	Bird* b=new Bird(mSceneMgr,mWorld,Vector3(200,100,0),node,flag_mat);

	node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    anim = mSceneMgr->createAnimation("CameraTrack2", 20);
    anim->setInterpolationMode(Animation::IM_SPLINE);
    track = anim->createNodeTrack(0, node);
    track->createNodeKeyFrame(0)->setTranslate(Vector3(50, 200, 0));
    track->createNodeKeyFrame(5)->setTranslate(Vector3(0, 150, 100));
    track->createNodeKeyFrame(10)->setTranslate(Vector3(100, 50, 0));
    track->createNodeKeyFrame(15)->setTranslate(Vector3(-20, 40, 30));
    track->createNodeKeyFrame(20)->setTranslate(Vector3(50, 200, 0));
    mAnimState = mSceneMgr->createAnimationState("CameraTrack2");
    mAnimState->setEnabled(true);

	b=new Bird(mSceneMgr,mWorld,Vector3(50,200,0),node,flag_mat);

	mSceneMgr->getEntity("Box23")->setRenderQueueGroup(RENDER_QUEUE_7);
	*lvlUp= new Lvl2Update(mSceneMgr,mWorld,grass,NULL);


}



void createLevel1(SceneManager* mSceneMgr, OgreNewt::World* mWorld, EnvironmentSounds* env,OgreNewt::MaterialID* flag_mat,LvlUpdate** lvlUp)
{
	mSceneMgr->setSkyBox(true, "TCENoonSkyBox");
		mWorld->setWorldSize(Vector3(-2000,-500,-2000),Vector3(2000,500,2000));
	loadScene("../../media/testxml.scene",mSceneMgr,mWorld,NULL,flag_mat);

	*lvlUp= new Lvl1Update();
}